How do you put an elephant on a containership in 3 steps?

PGConfEu Berlin 2022
2022-10-27

logo EDB

Who am I

Image by Anemone123 from Pixabay

Postgres on containers

  • What?
  • Why?
  • How?
  • Is this for me?
Image by Gerd Altmann from Pixabay
logo EDB

What

Containers are isolated from one another and bundle their own software, libraries and configuration files; they can communicate with each other through well-defined channels. Because all of the containers share the services of a single operating system kernel, they use fewer resources than virtual machines.
Image by MirelaSchenk from Pixabay
logo EDB

What

  • isolated
  • light weight
  • secured communications
  • encapsulation of all softwares, configuration and librairies
Image by monicore from Pixabay
logo EDB

Several technologies

  • Docker
  • LXC (LinuX Containers)
  • Podman
  • ...
Image by Ulrike Leone from Pixabay
logo EDB

is that all?

  • No
  • It goes deeper
  • Here comes... Kubernetes!
Image by Design_Miss_C from Pixabay
logo EDB

Cloud Native PG

  • Postgres Kubernetes operator
  • Apache 2.0 license
  • In the CNCF sanbox

Kubernetes

Kubernetes is an open-source container orchestration system for automating software deployment, scaling, and management.
Image by David Mark from Pixabay
logo EDB

Why?

  • Who has standalone Postgres nowadays?
  • Who manages less than 5 clusters in production?

➜ More things to do, less administrators

Image by Ian Lindsay from Pixabay
logo EDB

DBA Tasks

  • Installations/Configurations
  • Securization
  • Backup policy implementation
  • HA policy implementation
  • Minor/Major upgrades
  • Monitoring
  • Troubleshooting
logo EDB
https://dbaday.org/
Image by Gerd Altmann from Pixabay

Changing scripting into configuration

  • installation/configurations
  • securization
  • Backup/HA implementation
  • Minor upgrades

And more!

  • Self-healing
  • Everything in one place
  • Service encapsulation
  • Self-service for devs

ACID

  • Atomicity
  • Consistency
  • Isolation
  • Durability
Image by Andreas Lischka from Pixabay

Atomicity

Atomicity is a property that ensures that a database follows the all or nothing rule. In other words, the database considers all transaction operations as one whole unit or atom.
Source: Techopedia

Consistency

Consistency is a property ensuring that only valid data following all rules and constraints is written in the database. When a transaction results in invalid data, the database reverts to its previous state, which abides by all customary rules and constraints.
Source: Techopedia

Isolation

Isolation is a property that guarantees the individuality of each transaction, and prevents them from being affected from other transactions. It ensures that transactions are securely and independently processed at the same time without interference
Source: Techopedia
Image by Klaus Hausmann from Pixabay

Durability

Durability is a property that enforces completed transactions, guaranteeing that once each one of them has been committed, it will remain in the system even in case of subsequent failures.
Source: Techopedia
Image by RJA1988 from Pixabay

ACID

  • Atomicity
  • Consistency
  • Isolation
  • Durability
Image by Andreas Lischka from Pixabay

Stateless/statefull

The way containers are designed, and particularly the way Docker is designed, the assumption is that the container is stateless.
Mark Davis, ClusterHQ, 17 Jun 2015
Image by Niek Verlaan from Pixabay

Some tried early...

... And failed

It shouldn't be a reason not to try again!

Image by Esther Merbt from Pixabay

The key change

Kubernetes 1.14 and persistent volumes

Image by suju-foto from Pixabay

September 2021

70% of 500 executives and technology leaders are running stateful workloads in production with databases topping the list.
dok report 2021

How?

  • Declare your configuration
  • Deploy
  • Relax
Image by 은주 송 from Pixabay

Simple

# Example of PostgreSQL cluster
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: cluster-example
spec:
  instances: 3

  primaryUpdateStrategy: unsupervised
  storage:
    size: 1Gi
Image by 은주 송 from Pixabay

Specific Postgres version

apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
   # [...]
spec:
   # [...]
   imageName: ghcr.io/cloudnative-pg/postgresql:13.6
   #[...]
Image by 은주 송 from Pixabay

Backups

apiVersion: postgresql.cnpg.io/v1
kind: Cluster
# [...]
spec:
  backup:
    barmanObjectStore:
      destinationPath: "[destination path here]"
      s3Credentials:
        accessKeyId:
          name: aws-creds
          key: ACCESS_KEY_ID
        secretAccessKey:
          name: aws-creds
          key: ACCESS_SECRET_KEY
Image by 은주 송 from Pixabay

Settings

apiVersion: postgresql.cnpg.io/v1
kind: Cluster
# [...]
spec:
  postgresql:
    parameters:
      shared_buffers: "1GB"
      auto_explain.log_min_duration: "10s"
    pg_hba:
      - host laetitia laetitia all scram-sha-256
  # [...]
Image by 은주 송 from Pixabay

Resources

apiVersion: postgresql.cnpg.io/v1
kind: Cluster
# [...]
  resources:
    requests:
      memory: "32Mi"
      cpu: "50m"
    limits:
      memory: "128Mi"
      cpu: "100m"
  # [...]
Image by 은주 송 from Pixabay

Monitoring

apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
  name: cluster-example
spec:
  selector:
    matchLabels:
      postgresql: cluster-example
  podMetricsEndpoints:
  - port: metrics
Image by 은주 송 from Pixabay

Monitoring

apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: cluster-example
# [...]
spec:
# [...]
  monitoring:
    customQueriesConfigMap:
      - name: example-monitoring
        key: custom-queries
Image by 은주 송 from Pixabay

Migrating

apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: cluster-monolith
spec:
  bootstrap:
    initdb:
      import:
        type: monolith
        databases:
          - banking
        roles:
          - bank_user
        source:
          externalCluster: cluster-pg96
...
Image by 은주 송 from Pixabay

Migrating

...
  externalClusters:
    - name: cluster-pg96
      connectionParameters:
        # Use the correct IP or host name for the source database
        host: pg96.local
        user: postgres
        dbname: postgres
        sslmode: require
      password:
        name: cluster-pg96-superuser
        key: password
Image by 은주 송 from Pixabay

Rolling upgrades

    Triggered when...
  • Changing the imageName
  • Changing PostgreSQL configuration that needs a restart
  • A change on resources
  • A change on PVC on AKS
  • after an operator update
Image by 은주 송 from Pixabay

Rolling upgrades

    2 modes
  • Automated primaryUpdateStrategy:unsupervised
  • Manual primaryUpdateStrategy:supervised
Image by 은주 송 from Pixabay

Logs

  • CSV Format (on disk)
  • JSON format with kubectl logs
Image by 은주 송 from Pixabay

Is that for you?

It depends...

  • Is your team experimented with Kubernetes?
  • Is your team experimented with Postgres?
  • Do you manage more than 5 Postgres clusters?

Caution

Your Postgres database still needs

  • Data modeling
  • Correct SQL queries
  • Careful monitoring
Image by succo from Pixabay

Any questions?

Image by Andrew Martin from Pixabay